<?php 
class wisata extends CI_controller{

	function __construct(){
		parent::__construct();

		$this->load->helper('bara_helper');
		$this->load->library('bara_lib');

		$this->load->library('recaptcha');

		$this->load->model('modul/wisata_model');


		$this->load->model('modul/model_menu');

		$this->load->model('modul/about_model');
		$this->load->model('modul/article_model');
		$this->load->model('modul/sosmed_model');

	}

	function index($id=NULL){

		$data['text_home'] = "Home";
		$data['text_title'] = "Wisata";


		$data['base_url'] = base_url().'modul/wisata/index';
		$data['total_rows'] = $this->wisata_model->get_wisata_rows($this->bara_lib->language());
		$data['per_page'] = 16;

		$data['total_page'] = ceil($data['total_rows']/$data['per_page']);

		if($id==null){
			$id=1;
			$data['offset'] = 0; 
		} else {
			$id=$id;
			$data['offset'] = ($id-1) * $data['per_page'];	
		}  

		$data['posisi'] = $id;


		$data['gets_wisata'] = $this->wisata_model->get_wisata($this->bara_lib->language(),$data['per_page'],$data['offset']);


		$data['content'] = 'frontend/'. $this->bara_lib->theme() .'/modul/wisata/index';

		//menu
		$data['get_menus'] = $this->model_menu->get_menu($this->bara_lib->language());

		// sosmed
		$data['gets_facebook'] = $this->sosmed_model->get_sosmed('facebook');
		$data['gets_twitter'] = $this->sosmed_model->get_sosmed('twitter');
		$data['gets_sosmed'] = $this->sosmed_model->get_sosmed_all();

		//footer
		$data['gets_about_footer'] = $this->about_model->get_about('about_us',$this->bara_lib->language());
		$data['get_articles_footer'] = $this->article_model->get_article($this->bara_lib->language(),3,0);
		//end footer

		$data['head'] = 'layout/'. $this->bara_lib->theme() .'/head';
		$data['menu'] = 'layout/'. $this->bara_lib->theme() .'/menu';
		$data['footer'] = 'layout/'. $this->bara_lib->theme() .'/footer';
		$this->load->view('layout/'. $this->bara_lib->theme() .'/inner', $data);

	}

	function search($id=NULL){

		$data['text_home'] = "Home";
		$data['text_title'] = "Wisata";


		$data['base_url'] = base_url().'modul/wisata/index';
		$data['total_rows'] = $this->wisata_model->get_wisata_rows($this->bara_lib->language());
		$data['per_page'] = 16;

		$data['total_page'] = ceil($data['total_rows']/$data['per_page']);

		if($id==null){
			$id=1;
			$data['offset'] = 0; 
		} else {
			$id=$id;
			$data['offset'] = ($id-1) * $data['per_page'];	
		}  

		$data['posisi'] = $id;

		$keyword = $this->input->post('cari');

		if($keyword == 'Cari Wisata...') $keyword = "";
		else $keyword = $keyword;

		$provinsi = $this->input->post('provinsi');

		$data['gets_wisata'] = $this->wisata_model->get_wisata_search($this->bara_lib->language(),$keyword,$provinsi,$data['per_page'],$data['offset']);


		$data['content'] = 'frontend/'. $this->bara_lib->theme() .'/modul/wisata/index';

		//menu
		$data['get_menus'] = $this->model_menu->get_menu($this->bara_lib->language());

		// sosmed
		$data['gets_facebook'] = $this->sosmed_model->get_sosmed('facebook');
		$data['gets_twitter'] = $this->sosmed_model->get_sosmed('twitter');
		$data['gets_sosmed'] = $this->sosmed_model->get_sosmed_all();

		//footer
		$data['gets_about_footer'] = $this->about_model->get_about('about_us',$this->bara_lib->language());
		$data['get_articles_footer'] = $this->article_model->get_article($this->bara_lib->language(),3,0);
		//end footer

		$data['head'] = 'layout/'. $this->bara_lib->theme() .'/head';
		$data['menu'] = 'layout/'. $this->bara_lib->theme() .'/menu';
		$data['footer'] = 'layout/'. $this->bara_lib->theme() .'/footer';
		$this->load->view('layout/'. $this->bara_lib->theme() .'/inner', $data);

	}

	function view($id){

		$data['text_home'] = "Home";
		$data['text_title'] = "Wisata";

		//coment
		$this->recaptcha->recaptcha_check_answer();

		if ($this->input->post() && $this->recaptcha->getIsValid()) {
			
			$date = get_date_time();

			$data_comment = array(
				'id_wisata' => $id,
				'date_update' => $date,
				'name' => $this->input->post('name'),
				'email' => $this->input->post('email'),
				'url' =>$this->input->post('url'),
				'content' =>$this->input->post('comment'),
				'tipe' => 'wisata',
				'status' => 1
				);

			$id_menu = $this->db->insert('comment', $data_comment);
			
			$data['benar'] = "<div class='alert success'><i class='icon-success'></i> This is a Success Comment! <span class='alert-close fa-times'></span></div>";
        } else if ($this->input->post() && !$this->recaptcha->getIsValid()) {
        	$data['benar'] = "<div class='alert error'><i class='icon-error'></i> An Error Comment! <span class='alert-close fa-times'></span></div>";
        } else {
        	$data['benar'] = "";
        }

        $data['recaptcha_html'] = $this->recaptcha->recaptcha_get_html();

        $data['get_comments'] = $this->wisata_model->get_comment($id);
        $data['get_row_comment'] = $this->wisata_model->get_comment_row($id); 

        $data['get_foto_slide'] = $this->wisata_model->get_foto_slide($id);

		$data['get_wisata_views'] = $this->wisata_model->get_wisata_view($this->bara_lib->language(),$id);

		$data['get_rating'] = $this->wisata_model->get_rating($id);

		$data['content'] = 'frontend/'. $this->bara_lib->theme() .'/modul/wisata/view';
		

		//menu
		$data['get_menus'] = $this->model_menu->get_menu($this->bara_lib->language());
		
		// sosmed
		$data['gets_facebook'] = $this->sosmed_model->get_sosmed('facebook');
		$data['gets_twitter'] = $this->sosmed_model->get_sosmed('twitter');
		$data['gets_sosmed'] = $this->sosmed_model->get_sosmed_all();
		
		//footer
		$data['gets_about_footer'] = $this->about_model->get_about('about_us',$this->bara_lib->language());
		$data['get_articles_footer'] = $this->article_model->get_article($this->bara_lib->language(),3,0);
		//end footer
		
		$data['head'] = 'layout/'. $this->bara_lib->theme() .'/head';
		$data['menu'] = 'layout/'. $this->bara_lib->theme() .'/menu';
		$data['footer'] = 'layout/'. $this->bara_lib->theme() .'/footer';
		$this->load->view('layout/'. $this->bara_lib->theme() .'/inner', $data);	
	}

	function rate()
	{

		$date = get_date_time();

		$data_rate = array(
				'total_votes' => $this->input->post('value_rate'),
				'total_value' => $this->input->post('id_wisata'),
				'used_ips' => $_SERVER['REMOTE_ADDR'],
				'date' => $date
				);

		$id_menu = $this->db->insert('ratings', $data_rate);

		redirect('modul/wisata/view/' . $this->input->post('id_wisata'));
	}

	function ambildata(){

		$json = '{"wilayah": {';
		$json .= '"petak":[ ';
		foreach($this->wisata_model->get_wisata_all($this->bara_lib->language()) as $val){
		    $json .= '{';
		    $json .= '"id":"'.$val->id_wisata.'",
		             "judul":"'.htmlspecialchars($val->judul_wisata).'",
		             "deskripsi":"'.$val->judul_wisata.'",
		             "x":"'.$val->latitude_wisata.'",
		             "y":"'.$val->longitude_wisata.'",
		             "lokasi":"'.$val->alamat_wisata.'",
		             "jenis":"'.$val->nama_kategori_sub.'"
		             },';

		}

		$json = substr($json,0,strlen($json)-1);
		$json .= ']';

		$json .= '}}';
		echo utf8_encode($json);

	}


}
?>